﻿<?php
include_once("dbconnect.php");

function xemDanhMucCha() {
    $value = "";
    $getParent = "";
    $nameCha = $_POST['cate1'];
    $kq = mysqli_query(getConnect(), "select name from categories where id = (select parent from categories where name = '$nameCha')");
    while ($row = mysqli_fetch_array($kq)) {
        $getParent = "${row['parent']}";
    }
    if (strcmp($getParent, "null") === 0) {
        $value = "";
    } else {
        $rs = mysqli_query(getConnect(), "Select name from categories where parent = '$getParent' ");
        while ($hang = mysqli_fetch_array($rs)) {
            $value = $value . "${hang['name']}";
        }
    }
    echo $value;
}

function showTableDanhMuc() {
    $a = "<div class='row'>
                <div class='col-lg-12'>
                    <div class='panel panel-default'>
                        <div class='panel-heading'>
                            Các danh mục
                        </div>
                        <!-- /.panel-heading -->
                        <div class='panel-body'>
                            <div class='table-responsive'>
                                <table class='table table-striped table-bordered table-hover' id='dataTables-example'>
                                    <thead>
                                        <tr>
                                            <th>Họ tên</th>
                                            <th></th>                                            
                                        </tr>
                                    </thead>
                                    <tbody>";
    $result = mysqli_query(getConnect(), "Select * from categories");
    while ($row = mysqli_fetch_array($result)) {
        $a = $a . "<tr class='odd gradeX'>
                         <td>${row['name']}</td>
                         <td align ='center'><button class='btn  btn-primary' data-toggle='modal' data-target='#myModal' value='edit'>Chi tiết</button></td>                         
                      </tr>";
    }
    $a = $a . "   </tbody>
                  </table>
                </div> </div> </div> </div> </div>";
    echo $a;
}

function showManu() {
    $showManu = "";
    $kq = mysqli_query(getConnect(), "Select * from manufacturers");
    while ($row = mysqli_fetch_array($kq)) {
        $showManu = $showManu . "<option>${row['name']}</option>";
    }
    echo $showManu;
}

function showCate() {
    $showCate = "<option></option>";
    $kq = mysqli_query(getConnect(), "Select * from categories");
    while ($row = mysqli_fetch_array($kq)) {
        $showCate = $showCate . "<option>${row['name']}</option>";
    }
    echo $showCate;
}

//function printTable() {
////$q = intval($_GET['q']);
//    $cate = isset($_POST['cate1']) ? $_POST['cate1'] : '';
////$cate = $_POST['cate1'];
//    $str = "<div class='row'>
//                <div class='col-lg-12'>
//                    <div class='panel panel-default'>
//                        <div class='panel-heading'>
//                            Danh sách nhà sản xuất
//                        </div>
//                        <!-- /.panel-heading -->
//                        <div class='panel-body'>
//                            <div class='table-responsive'>
//                                <table class='table table-striped table-bordered table-hover' id='dataTables-example'>
//                                    <thead>
//                                        <tr>
//                                            <th>Category</th>
//                                            <th>Manufacturer</th>
//											 <th>EDIT</th>
//                                        </tr>
//                                    </thead>
//                                    <tbody>";
//    //	" . $cate . "
//    //$query = "Select * from cate_manu where category = 'Mainboard'"  ;
//    $query = "SELECT * FROM cate_manu WHERE category = ( SELECT id FROM categories WHERE name =  'Mainboard' ) ";
//    $result = mysqli_query(getConnect(), $query)
//            or die("Error: " . mysqli_error(getConnect()));
//    while ($row = mysqli_fetch_array($result)) {
//        $str = $str . "     <tr class='odd gradeX'>
//                                <td>${row['category']}</td>
//                                <td>${row['manufacturer']}</td>
//                                <td align ='center'><i class='fa fa-pencil-square-o fa-3x' data-toggle='modal' data-target='#myModal' value='edit'></i></td>
//                            </tr>";
//    }
//    $str = $str . " </tbody>
//                  </table>
//                </div> ";
//    echo $str;
//}

if (isset($_POST['parentCate'])) {
    $cate = $_POST['parentCate'];
    $str = "";
    $kq = mysqli_query(getConnect(), "select name from categories where id = (select parent from categories where name = '$cate')");
    while ($row = mysqli_fetch_array($kq)) {
        $str = $str. "{$row['name']}";
    }
    echo  $str;
}

if (isset($_POST['cateToManu'])) {
    $str = "";
   
    $ct_id = ($_POST['cateToManu']);
    $type = ($_POST['type']);
    $str = "<div class='row'>
                <div class='col-lg-12'>
                    <div class='panel panel-default'>
                       
                        <!-- /.panel-heading -->
                        <div class='panel-body'>
                            <div class='table-responsive'>
                                <table class='table table-striped  table-hover' id='dataTables-example'>
                                    <thead>
                                        <tr>
                                            <th style='display:none'>Category</th>
                                            <th>Manufacturer</th>
											 <th style='display:none'>EDIT</th>
                                        </tr>
                                    </thead>
                                    <tbody>";
    $query = "SELECT categories.name AS Cname, manufacturers.name AS Mname
FROM  `cate_manu` 
INNER JOIN categories ON categories.id = cate_manu.category
INNER JOIN manufacturers ON manufacturers.id = cate_manu.manufacturer
WHERE categories.name =  '" . $ct_id . "'";

    $result = mysqli_query(getConnect(), $query);
    $count = 1;
    while ($row = mysqli_fetch_array($result)) {
        $str = $str . "     <tr class='odd gradeX'>
                                            <td style='display:none'>${row['Cname']}</td>
                                            <td>${row['Mname']}</td>
											<td style='display:none' class=view' align ='center'><i class='fa fa-pencil-square-o fa-3x' data-toggle='modal' data-target='#myModal' value='edit' id='" . $count . "' onclick='view(this)';></i></td>
                                        </tr>";
        $count++;
    }

    $str = $str . " </tbody>
                                </table>
                            </div> ";
    echo $str;
}

function insert($cateCha) {
    $name = $_POST['name'];
    $query = "insert into categories (name, parent) values ('$name',$cateCha)";
    mysqli_query(getConnect(), $query);
    header("Location: ../categories.php?status=createSuccess");
}

function update($danhMucCha2) {
    $tenMoi = $_POST['tenMoi'];
    $tenCu = $_POST['tenCu'];
    $query = "UPDATE categories SET name ='$tenMoi', parent = '$danhMucCha2' WHERE name = '$tenCu'";
    $result = mysqli_query(getConnect(), $query);

    if ($result) {
        header("Location: ../categories.php?status=updateSuccess");
    } else {
        die('Update failed ' . mysqli_error($result));
    }
}

function delete() {
    $tenCu = $_POST['tenCu'];
    $query = "DELETE FROM categories  WHERE name='$tenCu'";
    $result = mysqli_query(getConnect(), $query);
    if ($result) {
        header("Location: ../categories.php?status=deleteSuccess");
    } else {
        die('Delete failed ' . mysqli_error($result));
    }
}

function addManu() {
    $nameCate = $_POST['cate'];
    $nameManu = $_POST['manu'];
    $query = "insert into cate_manu values ((select id from categories where name='$nameCate'),(select id from manufacturers where name='$nameManu'));";
    mysqli_query(getConnect(), $query);
    header("Location: ../categories.php?status=addManuSuccess");
}

function deleteManu() {
    $nameCate = $_POST['cate'];
    $nameManu = $_POST['manu'];
    $query = "delete from cate_manu where category=(select id from categories where name='$nameCate') and manufacturer=(select id from manufacturers where name='$nameManu')";
    mysqli_query(getConnect(), $query);
    header("Location: ../categories.php?status=deleteManuSuccess");
}

if (isset($_POST['edit'])) {
    if ($_POST['edit'] == "Thêm danh mục") {
        $dmCha = $_POST['danhMucCha'];
        if (strcmp($dmCha, "") === 0) {
            $insertDanhMucCha = "null";
            insert($insertDanhMucCha);
        } else {
            $kq = mysqli_query(getConnect(), "Select id from categories where name = '$dmCha'");
            while ($row = mysqli_fetch_array($kq)) {
                $insertDanhMucCha = "'${row['id']}'";
            }
            insert($insertDanhMucCha);
        }
    } elseif ($_POST['edit'] == "Cập nhật") {
        $dmCha2 = $_POST['cateCha2'];
        $tenMoi = $_POST['tenMoi'];
        $tenCu = $_POST['tenCu'];
        if (strcmp($tenMoi, "") === 0) {
            if(strcasecmp($dmCha2, "")===0){
                header("Location: ../categories.php?status=updateFailed");
            }else{
                $result = mysqli_query(getConnect(), "Select id from categories where name = '$dmCha2'");
                while ($row2 = mysqli_fetch_array($result)) {
                    $updateDanhMucCha = "${row2['id']}";
                }
                $query = "UPDATE categories SET parent = '$updateDanhMucCha' WHERE name = '$tenCu'";
                $result2 = mysqli_query(getConnect(), $query);
                if ($result2) {
                    header("Location: ../categories.php?status=updateSuccess");
                } else {
                    header("Location: ../categories.php?status=updateFailed");
                }
            }
        } else {
            if (strcmp($dmCha2, "") === 0) {
                $query = "UPDATE categories SET name ='$tenMoi' WHERE name = '$tenCu'";
                $result = mysqli_query(getConnect(), $query);
                if ($result) {
                    header("Location: ../categories.php?status=updateSuccess");
                } else {
                    header("Location: ../categories.php?status=updateFailed");
                }
            } else {
                $kq2 = mysqli_query(getConnect(), "Select id from categories where name = '$dmCha2'");
                while ($row2 = mysqli_fetch_array($kq2)) {
                    $updateDanhMucCha = "${row2['id']}";
                }
                $query = "UPDATE categories SET name ='$tenMoi', parent = '$updateDanhMucCha' WHERE name = '$tenCu'";
                $result = mysqli_query(getConnect(), $query);
                if ($result) {
                    header("Location: ../categories.php?status=updateSuccess");
                } else {
                    header("Location: ../categories.php?status=updateFailed");
                }
            }
        }
    } elseif ($_POST['edit'] == "Thêm nhà sản xuất") {
        addManu();
    } elseif ($_POST['edit'] == "Xóa nhà sản xuất") {
        deleteManu();
    } elseif ($_POST['edit'] == "Xóa") {
        delete();
    }
}
?>

